import React, {Component} from 'react'
import {Input, List, message} from "antd";
import Device from "../../../types/Device";
import {listDevice, queryDeviceAmount, searchDevice} from "../../../requests/Device"
import DeviceItem from "../item/DeviceItem"
import {getRes} from "../../../configFile";

const { Search } = Input;

//props接口
interface IProps { }

interface IState {
    DeviceSource: Array<Device>
    amount: number
    offset: number
    limit: number
    isSearch: boolean
}

export default class DeviceshowList extends Component<IProps, IState> {
    constructor(props: IProps) {
        super(props)
        this.state = {
            DeviceSource: [],
            amount: 0,
            offset: 0,
            limit: 10,
            isSearch: false
        }
    }

    onSearch: (values: string) => void = async values => {
        if(values===''){
            await this.getDevice();
        }else{
            try {
                const res = getRes(await searchDevice(values))
                if (!res.isSuccessful) {
                    message.error("查询失败")
                }
                this.setState({
                    ...this.state,
                    DeviceSource: res.data,
                    amount: res.data.length,
                    isSearch: true
                })
            } catch (err) {
                message.error(err.toString())
            }
        }
    }

    //获取设备数据
    getDevice = async () => {
        try {
            const amountRes = getRes(await queryDeviceAmount())
            const res = getRes(await listDevice(this.state.offset, this.state.limit))

            if (amountRes.isSuccessful && res.isSuccessful) {
                this.setState({
                    DeviceSource: res.data,
                    amount: amountRes.data
                })
            } else {
                message.error("获取失败")
            }
        }
        catch (error) {
            message.error(error.toString())
        }
    }

    componentDidMount() {
        this.getDevice()
    }

    //分页变化调用函数
    paginationChange = (page: number, pageSize: number | undefined) => {
        if (!!pageSize) {
            this.setState({
                offset: (page - 1) * (pageSize),
                limit: (pageSize)
            },()=>{
                this.getDevice()
            })
        }
    }
    render() {
        return (
            <div className='DeviceList'>
                <Search placeholder="搜索" onSearch={values => this.onSearch(values)} allowClear={true} enterButton />
                <List
                    size="large"
                    pagination={{
                        onChange: this.state.isSearch ? undefined : this.paginationChange,
                        defaultPageSize: this.state.limit,
                        total: this.state.amount,
                        showSizeChanger: false
                    }}
                    dataSource={this.state.DeviceSource}
                    renderItem={item => (
                        <DeviceItem key={item.deviceID} device={item} />
                    )}
                />
            </div>
        )
    }
}
